package com.yuexunit.finance.project.action.v1;


import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;

import com.google.inject.Singleton;
import com.yuexunit.finance.project.action.v1.model.WarningSchemeResult;
import com.yuexunit.inject.ComponentFactory;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.web.annotation.ActionServlet;
import com.yuexunit.web.annotation.Permission;

import net.sf.jxls.transformer.XLSTransformer;

/**
 *@version 创建时间：2017年6月27日 下午2:29:56
 *@author wenwenlong
 *类说明
 */

@ActionServlet({"/api/[^/]+/ExportSchemeResultInfo.json"})
@Singleton
@Permission
public class ExportSchemeResultInfo extends HttpServlet{

	private static final long serialVersionUID = 1L;

	private FinanceProductActionApi financeProductActionApi = ComponentFactory.loadClass(FinanceProductActionApi.class);

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response){
		try {
			String factor = null;
			String settlementState = null;
			String financeContractSerialNumber = null;
			String financeNumber = null;
			String enterpriseName = null;
			Integer productType = null;
			
			if (!"undefined".equals(request.getParameter("factor")) && StringUtils.isNotBlank(request.getParameter("factor"))) {
				factor = request.getParameter("factor");
			}
			if (!"undefined".equals(request.getParameter("settlementState")) && StringUtils.isNotBlank(request.getParameter("settlementState"))) {
				settlementState = request.getParameter("settlementState");
			}
			if (!"undefined".equals(request.getParameter("financeContractSerialNumber")) && StringUtils.isNotBlank(request.getParameter("financeContractSerialNumber"))) {
				financeContractSerialNumber = request.getParameter("financeContractSerialNumber");
			}
			if (!"undefined".equals(request.getParameter("financeNumber")) && StringUtils.isNotBlank(request.getParameter("financeNumber"))) {
				financeNumber = request.getParameter("financeNumber");
			}
			if (!"undefined".equals(request.getParameter("enterpriseName")) && StringUtils.isNotBlank(request.getParameter("enterpriseName"))) {
//				enterpriseName = URLDecoder.decode(new String(request.getParameter("enterpriseName").getBytes("ISO-8859-1"),"UTF-8"));
				enterpriseName = URLDecoder.decode(new String(request.getParameter("enterpriseName")));
			}
			if (!"undefined".equals(request.getParameter("productType")) && StringUtils.isNotBlank(request.getParameter("productType"))) {
				productType = Integer.parseInt(request.getParameter("productType"));
			}

			PageCriteria page = PageCriteria.defaultInstance();
			page.setPageSize(100000);
			System.out.println(productType);
			System.out.println(enterpriseName);
			System.out.println(financeNumber);
			System.out.println(financeContractSerialNumber);
			System.out.println(settlementState);
			System.out.println(factor);
			PageList<WarningSchemeResult> wsrs = financeProductActionApi.inquireWarningSchemeResultPageList(Integer.parseInt(factor), null, null, Integer.parseInt(settlementState), financeContractSerialNumber, financeNumber, enterpriseName, productType, page);
			Map<String,Object> beans = new HashMap<String,Object>();  
			beans.put("arr", wsrs);  
			XLSTransformer transformer = new XLSTransformer();  

			InputStream in=null;  
			OutputStream out=null;  
			//设置响应 
			response.setHeader("Content-Disposition", "attachment;filename=" + new String(("预警信息信息.xls").getBytes("GB2312"), "ISO_8859_1"));  
			response.setContentType("application/vnd.ms-excel");

			in = new BufferedInputStream(new FileInputStream("C:\\Finance\\upload\\update\\warning_scheme_ressult_array"+factor+".xls"));  
			Workbook workbook = transformer.transformXLS(in, beans);
			out=response.getOutputStream();  
			workbook.write(out);  
			out.flush();
			if (in != null) {
				in.close();
			}
			if (out != null) {
				out.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) {
		this.doPost(request, response);
	}

}
